/**
 * FileName: FileHelper.java
 * @Description: TODO(用一句话描述该文件做什么)
 * All rights Reserved, Designed By 21winmess
 * Copyright: Copyright(C) 2010-2011 
 * Company 21winmess DaLian LTD.
 * @author: Matrix xu
 * @version V2.0
 * Createdate: 2012-12-3 上午11:27:57
 *
 * Modification History:
 * Date Author Version Discription
 * -----------------------------------------------------------------------------------
 * 2012-12-3 Qute_Android2.0
 * Why & What is modified: <修改原因描述>
 */
package com.me.safebox.utils;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

import net.tsz.afinal.bitmap.core.FileNameGenerator;

import android.os.Environment;

import com.me.safebox.service.FileConfig;

/**
 * @ClassName: FileHelper
 * @Description:文件操作帮助类
 * @author: Matrix
 * @date: 2012-12-3 上午11:27:57
 * 
 */
public class FileHelper {

	public static void createSourceFolder(int folderType) {
		MountInfo mountInfo = SdUtil.checkMountInfo();// 检查SD卡挂载分区
		String encrptFolder = "";
		if (mountInfo.isInternalsdAvaliable) {// 内置SD是否正常挂载
			encrptFolder = getEncrptFolder(Environment
					.getExternalStorageDirectory().getPath(), folderType);
			File encrptFolderDir = new File(encrptFolder);
			if (!encrptFolderDir.exists()) {
				encrptFolderDir.mkdirs();
			}
		}
		if (mountInfo.isExternalSdAvaliable) {// 外置SD是否正常挂载
			encrptFolder = getEncrptFolder(mountInfo.externalSdPath, folderType);
			File encrptFolderDir = new File(encrptFolder);
			if (!encrptFolderDir.exists()) {
				encrptFolderDir.mkdirs();
			}
		}

	}

	/**
	 * 根据文件绝对路径，获得该文件加密后的存储地址 如果是文件存在内置SD卡,那么加密后文件也存在内置SD 如果文件存在外置SD...
	 * 
	 * @param fileAbsPath
	 * @return
	 */
	public static String getEncryptFileDesFolder(String fileAbsPath,
			int fileType) {
		String sdRoot = "";

		if (fileAbsPath.startsWith(MountInfo.externalSdPath)) {
			sdRoot = MountInfo.externalSdPath;
		} else if (fileAbsPath.startsWith(Environment
				.getExternalStorageDirectory().getPath())) {
			sdRoot = Environment.getExternalStorageDirectory().getPath();
		}

		return getEncrptFolder(sdRoot, fileType);
	}

	public static String[] getFileName(String filePath) {
		String[] array = new String[2];
		File file = new File(filePath);
		String fileName = file.getName();
		String sourceDir = file.getParent();
		array[0] = fileName;
		array[1] = sourceDir;
		file = null;
		return array;
	}

	/**
	 * 移动文件多态模式
	 * 
	 * @param fromPath
	 * @param toPath
	 * @return
	 */
	public static String moveFile(String fromPath, String toDir, String newName) {
		File fromFile = new File(fromPath);
		File toFile = new File(toDir, newName);
		if (fromFile.renameTo(toFile)) {
			return toFile.getAbsolutePath();
		}
		return null;
	}

	/**
	 * 移动文件
	 * 
	 * @param fileAbsPath
	 * @param desFolderDir
	 *            外置SD卡目录 内置SD卡目录
	 * @return
	 */
	public static String moveFile(String fileAbsPath, int fileType) {
		String desFolderDir = getEncryptFileDesFolder(fileAbsPath, fileType);// 这句有点小问题,其实是优化的问题,不需要每次移动文件都检查，在外部检查一次就好，
		String newName = FileNameGenerator.generator(UUID.randomUUID()
				.toString());
		return moveFile(fileAbsPath, desFolderDir, newName);
	}

	public static boolean createFile(String fileName, String fileDir) {
		File file = new File(fileName);
		if (!file.exists()) {
			try {
				File dir = new File(fileDir);
				if (!dir.exists()) {
					if (!dir.mkdirs())
						return false;
				}
				return file.createNewFile();
			} catch (IOException e) {
				return false;
			}
		}
		return true;
	}

	/**
	 * 根据文件类型获得私密文件目录
	 * 
	 * @param fileType
	 * @return
	 */
	private static String getEncrptFolder(String SDRoot, int fileType) {
		switch (fileType) {
		case FileConfig.FILE_TYPE_IMAGE:
			return SDRoot + FileConfig.ENCRPT_IMAGE_FOLDER_PATH;
		}
		return FileConfig.ENCRPT_IMAGE_FOLDER_PATH;
	}

	public boolean encryFile() {
		return false;
	}

	public boolean decryFile() {
		return false;
	}
}
